﻿/* BUG: stack smashing BUG using memcpy() */
/* memcpy() not safe */
#include "defines.h"

#define SEG_TABLE_INIT_NR	64
#define SEG_ALLOC_SUCCESS	0
#define SEG_RELEASE_SUCCESS	0
#define SEG_LOAD_SUCCESS	0
#define SEG_SAVE_SUCCESS	0
#define SEG_GET_SUCCESS		0
#define	SEG_GET_FAIL		-1
#define	SEG_FREE_SUCCESS	0
#define	SEG_FREE_FAIL		-1

typedef struct {
	int table_size;

	int page_size;
	int iterm_size;
	int iterm_nr_in_page;

	int iterm_horizon;
	int cur_page_id;
	char* seg[SEG_TABLE_INIT_NR];  
} SEG_TABLE;

static void _seg_alloc_page(SEG_TABLE** seg, int page_id);

SEG_TABLE* seg_create(int seg_table_size, int page_size, int iterm_size);
STATE  seg_free(SEG_TABLE* seg);		


/* iterm_cnt,  cur_page_id */
/* use before seg_create() */
void seg_load(SEG_TABLE** seg_table, PTR_DISK _start, int seg_max_page_id);
void seg_save(SEG_TABLE* seg_table, PTR_DISK _start);


/* before use, check init: 
seg->iterm_cnt
seg->cur_page_id
*/
STATE  seg_get_iterm(SEG_TABLE*  seg, char* iterm); 
void  seg_put_iterm(SEG_TABLE** seg, char* iterm);
void* seg_get_ptr(SEG_TABLE* seg); /* XXX: multipthread */

